import { defineConfig, loadEnv } from "vite"
import react from "@vitejs/plugin-react"
import path from "path"
import proxy from "./src/http/proxy"
import viteEslint from "vite-plugin-eslint"
import viteCompression from "vite-plugin-compression"
import UnoCSS from "unocss/vite"
// https://vitejs.dev/config/
const pathSrc = path.resolve(__dirname, "src")
export default ({ mode }) => {
  const env = loadEnv(mode, process.cwd())
  console.log("当前环境", env)
  return defineConfig({
    plugins: [
      UnoCSS(),
      react(),
      viteCompression({
        algorithm: "gzip",
        threshold: 512000,
        ext: ".gz"
      }),
      {
        ...viteEslint(),
        apply: "build"
      },
      {
        ...viteEslint({
          failOnWarning: false,
          failOnError: false
        }),
        apply: "serve",
        enforce: "post"
      }
    ],
    resolve: {
      alias: {
        "@": pathSrc
      }
    },
    css: {
      preprocessorOptions: {
        less: {
          additionalData: `@import "${path.resolve(__dirname, "src/assets/styles/pxTovwvh.less")}";`
        }
      }
    },
    base: "./",
    build: {
      target: "modules",
      outDir: "demo-platform-web", //项目打包文件夹名称, 如nginx 使用root 配置,需与 location 的值一致
      chunkSizeWarningLimit: 2048,
      minify: "terser",
      terserOptions: {
        compress: {
          drop_console: true,
          drop_debugger: true
        }
      },
      rollupOptions: {
        output: {
          manualChunks: {
            antd: ["antd"],
            echarts: ["echarts"]
          }
        }
      }
    },
    server: {
      proxy: {
        ...proxy
      }
    }
  })
}
